<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="closefn" @open="openPremiss">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds" v-loading="loading">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      <!-- <el-checkbox label="113">开发者</el-checkbox>
      <el-checkbox label="115">人事</el-checkbox> -->
    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="changefn">确定</el-button>
      <el-button size="small" @click="closefn">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { ChangeUpdataUSerInfoAPI } from '@/api/emploees'
import { getUserInfoAPI } from '@/api/setting'
import { getUserAPI } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleIds: [],
      list: [],
      loading: false
    }
  },
  methods: {
    closefn() {
      this.$emit('update:showRoleDialog', false)
    },
    async openPremiss() {
      this.loading = true
      const { data: { rows }} = await getUserInfoAPI()
      console.log(rows)
      this.list = rows

      const res = await getUserAPI(this.userId)
      console.log(res)
      this.roleIds = res.data.roleIds || []

      this.loading = false
    },
    async changefn() {
      const res = await ChangeUpdataUSerInfoAPI({
        id: this.userId,
        roleIds: this.roleIds
      })
      console.log(res)
      this.$message.success('分配成功')
      this.closefn()
    }
  }
}
</script>
